import httpRequest from "./http";
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 * @param {String} type [请求时携带的参数]
 */
export function get(url, params, type = "") {
  httpRequest.defaults.headers.get["Content-Type"] =
    type == "" ? "application/json" : type;
  return new Promise((resolve, reject) => {
    httpRequest
      .get(url, {
        params: params,
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 * @param {String} type [请求时携带的参数]
 */
 export function post(url, params, type = "") {
  return new Promise((resolve, reject) => {
    // httpRequest.post(url, QS.stringify(params))
    httpRequest.defaults.headers.post["Content-Type"] =
      type == "" ? "application/json" : type;
    httpRequest
      .post(url, params)
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

/**
 * put方法，对应put请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 * @param {String} type [请求时携带的参数]
 */
 export function put(url, params, type = "") {
  return new Promise((resolve, reject) => {
    // httpRequest.post(url, QS.stringify(params))
    httpRequest.defaults.headers.put["Content-Type"] =
      type == "" ? "application/json" : type;
    httpRequest
      .post(url, params)
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
